﻿using System.Collections.Generic;

namespace dk.Helper.Base.Core
{
    public class Players : IPlayers
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="T:System.Object"/> class.
        /// </summary>
        public Players()
        {
            PlayersByID = new Dictionary<int, IPlayer>();
            PlayersByName = new Dictionary<string, IPlayer>();
            PlayersByAllyID = new Dictionary<int, IList<IPlayer>>();
        }

        #region IPlayers Members

        /// <summary>
        /// Gets the players by ID.
        /// </summary>
        public IDictionary<int, IPlayer> PlayersByID { get; private set; }

        /// <summary>
        /// Gets the players by name.
        /// </summary>
        public IDictionary<string, IPlayer> PlayersByName { get; private set; }

        /// <summary>
        /// Gets the players by ally ID.
        /// </summary>
        public IDictionary<int, IList<IPlayer>> PlayersByAllyID { get; private set; }

        /// <summary>
        /// Gets a value indicating whether this instance is empty.
        /// </summary>
        /// <value>
        ///   <c>true</c> if this instance is empty; otherwise, <c>false</c>.
        /// </value>
        public bool IsEmpty
        {
            get { return Values.Count == 0; }
        }

        /// <summary>
        /// Gets the values.
        /// </summary>
        public ICollection<IPlayer> Values
        {
            get { return PlayersByID.Values; }
        }

        /// <summary>
        /// Adds the specified player.
        /// </summary>
        /// <param name="player">The player.</param>
        public void Add(IPlayer player)
        {
            if (!PlayersByID.ContainsKey(player.ID))
                PlayersByID.Add(player.ID, player);

            if (!PlayersByName.ContainsKey(player.Name))
                PlayersByName.Add(player.Name, player);

            if (!PlayersByAllyID.ContainsKey(player.AllyID))
                PlayersByAllyID.Add(player.AllyID, new List<IPlayer>());

            if (!PlayersByAllyID[player.AllyID].Contains(player))
                PlayersByAllyID[player.AllyID].Add(player);
        }

        /// <summary>
        /// Determines whether this instance contains the specified player.
        /// </summary>
        /// <param name="player">The player.</param>
        /// <returns>
        ///   <c>true</c> if this instance contains the specified player; otherwise, <c>false</c>.
        /// </returns>
        public bool Contains(IPlayer player)
        {
            return PlayersByID.ContainsKey(player.ID);
        }

        /// <summary>
        /// Removes the specified player.
        /// </summary>
        /// <param name="player">The player.</param>
        public void Remove(IPlayer player)
        {
            if (PlayersByID.ContainsKey(player.ID))
                PlayersByID.Remove(player.ID);
            if (PlayersByName.ContainsKey(player.Name))
                PlayersByName.Remove(player.Name);
            if (PlayersByAllyID.ContainsKey(player.AllyID))
                if (PlayersByAllyID[player.AllyID].Contains(player))
                    PlayersByAllyID[player.AllyID].Remove(player);
        }

        #endregion
    }
}